/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.objects.extra;

import java.util.Vector;

import org.easyway.interfaces.base.IDestroyable;
import org.easyway.interfaces.base.IPureRender;
import org.easyway.objects.sprites2D.Sprite;
import org.easyway.utils.Utility;
import org.lwjgl.opengl.GL11;

/**
 * @author Daniele Paggi
 * 
 */
@Deprecated
public class StaticObject implements IPureRender, IDestroyable {
	/** last list free */
	private static int lastmylist = 0;

	/** list used */
	private int mylist = -1;

	/** size of static Image */
	private int width, height;

	/** indicates if the object is destryed or not */
	private boolean destroyed = false;

	/** lists of Sprites that used the current static image */
	private Vector<Sprite> used = new Vector<Sprite>();

	/**
	 * create a new instance of static object
	 * 
	 * @param spr
	 *            the sprite to do static
	 */
	public StaticObject(Sprite spr) {
		if (spr != null) {
			makeStaticFrom(spr);
			spr.setStatic(this);
		} else {
			Utility.error("StaticObj( Sprite ): "
					+ "can't crate a Static Image from a null Sprite",
					"StaticObj");
			destroy();
		}
	}

	private void makeStaticFrom(Sprite spr) {
		// search a free slot
		mylist = lastmylist + 1;
		while (GL11.glIsList(mylist)) {
			mylist++;
		}
		lastmylist = mylist;
		this.width = spr.getWidth();
		this.height = spr.getHeight();
		// make the new list
		GL11.glNewList(mylist, GL11.GL_COMPILE);
		GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(0, 0, 0);
		GL11.glTexCoord2f(1, 0);
		GL11.glVertex3f(spr.getWidth(), 0, 0);
		GL11.glTexCoord2f(1, 1);
		GL11.glVertex3f(spr.getWidth(), spr.getHeight(), 0);
		GL11.glTexCoord2f(0, 1);
		GL11.glVertex3f(0, spr.getHeight(), 0);
		GL11.glEnd();
		GL11.glEndList();
	}

	public void onDestroy() {
	}

	/** destroy the Static Image */
	public void destroy() {
		if (destroyed)
			return;
		onDestroy();
		destroyed = true;
		Sprite temp;
		for (int i = used.size() - 1; i >= 0; i--) {
			temp = (Sprite) used.get(i);
			if (temp != null)
				temp.setStatic(null);
			used.remove(i);
		}
		used = null;
		GL11.glDeleteLists(mylist, 0);
		if (lastmylist > mylist)
			lastmylist = mylist;
	}

	/** remove a sprite from the list */
	public void remove(Sprite spr) {
		if (used.contains(spr))
			used.remove(spr);
		else
			Utility.error("List of StaticObject don't "
					+ "contain \"the sprite\" indicates",
					"StaticObj.remove(Spr)");
	}

	/** adds an sprite to the StaticObject */
	public void add(Sprite spr) {
		if (spr != null)
			if (used.contains(spr)) {
				Utility.error("add(Sprite) in StaticObj : "
						+ "the sprite is already contained",
						"StaticObj.add(Spr)#contained");
			} else {
				used.add(spr);
				spr.setSize(width, height);
			}
		else
			Utility.error("add(Sprite) in StaticObj : "
					+ "can't add a null Sprite", "StaticObj.add(Spr)#null");
	}

	/** indicates if the static image is destroyed or not */
	public boolean isDestroyed() {
		return destroyed;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Drawing#render()
	 */
	public void render() {
		if (GL11.glIsList(mylist))
			GL11.glCallList(mylist);
		else
			Utility.error("CORE BUG: render() in StaticObject: "
					+ "can't call a list not used..", "StaticObj.render()");
	}


}
